/*
 Copyright (C) 2010 QDSS.org
 
 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.qdss.bizz.security.member;

import java.io.Serializable;
import java.security.Principal;
import java.security.acl.Group;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Set;

/**
 * 成员组
 * 
 * @author <a href="mailto:zhaofang123@gmail.com">Zhao Fangfang</a>
 * @since 0.2, 2010-10-4
 * @version $Id: MemberGroup.java 7 2011-05-15 16:40:50Z zhaofang123@gmail.com $
 */
public class MemberGroup extends Member implements Group {
	private static final long serialVersionUID = 4050659655955435119L;
	
	/**
	 * 成员 */
	protected final Set<Principal> allMembers = new HashSet<Principal>();
	
	/**
	 * @param identity
	 * @param name
	 * @param disabled
	 */
	public MemberGroup(Serializable identity, String name, boolean disabled) {
		super(identity, name, disabled);
	}

	@Override
	public boolean addMember(Principal user) {
		if (isMember(user))
			return false;
		return allMembers.add(user);
	}
	
	@Override
	public boolean removeMember(Principal user) {
		if (!isMember(user))
			return false;
		return allMembers.remove(user);
	}
	
	@Override
	public boolean isMember(Principal user) {
		return allMembers.contains(user);
	}
	
	@Deprecated
	@Override
	public Enumeration<? extends Principal> members() {
		throw new UnsupportedOperationException("Deprecated method! Please using #getMembers");
	}

	/**
	 * 获取成员列表
	 * 
	 * @return
	 */
	public Set<Principal> getMembers() {
		return Collections.<Principal>unmodifiableSet(allMembers);
	}
}
